//
//  LJP_SearchViewController.m
//  God_is_a_Girl
//
//  Created by 李继鹏 on 15/9/28.
//  Copyright (c) 2015年 呸. All rights reserved.
//

#import "LJP_SearchViewController.h"
#import "LJP_SearchHeader.h"
#import "LJP_searchCollectionViewCell.h"
#import "DestinationCollectionReusableView.h"
#import "LJP_searchRecomModel.h"
#import "LHQDestinationDetailViewController.h"
#import "LJP_SearchResultViewController.h"
@interface LJP_SearchViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UISearchBarDelegate>
@property (nonatomic, retain) LJP_SearchHeader *searchHeader;
@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain) NSMutableArray *titleArray;
@property (nonatomic, retain) NSMutableArray *upArray;
@property (nonatomic, retain) NSMutableArray *downArray;
@end

@implementation LJP_SearchViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}
- (void)collectionViewSubview
{
    
    //创建瀑布流
    self.flowLayout = [[UICollectionViewFlowLayout  alloc] init];
    //创建对象
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:self.flowLayout];
    //方块大小
    self.flowLayout.itemSize = CGSizeMake(100, 30);
    self.flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width,70);
    //最小列间距
    self.flowLayout.minimumInteritemSpacing = 10 ;
    //最小行间距
    self.flowLayout.minimumLineSpacing = 10 ;
    //设置item据四边的间距
    self.flowLayout.sectionInset = UIEdgeInsetsMake(15 , 15 , 15 , 15);
    [self.view  addSubview:self.collection];
    self.collection.backgroundColor = [UIColor whiteColor];
    //两个协议
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.backgroundColor = [UIColor colorWithRed:1.000 green:0.950 blue:0.841 alpha:1.000];
    //collectionViewcell 必须提前注册
    [self.collection registerClass:[LJP_searchCollectionViewCell class] forCellWithReuseIdentifier:@"qwe"];
    //标题
    [self.collection  registerClass:[DestinationCollectionReusableView  class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"sectionTitle"];
}
- (void)dataHandle
{
    self.titleArray = [NSMutableArray array];
    self.upArray = [NSMutableArray array];
    self.downArray = [NSMutableArray array];
    for (NSDictionary *dic in self.dataArray) {
        NSString *title = [dic objectForKey:@"title"];
        [self.titleArray addObject:title];
    }
    NSArray *upArray = [[self.dataArray firstObject] objectForKey:@"elements"];
    NSArray *downArray = [[self.dataArray lastObject] objectForKey:@"elements"];
    for (NSDictionary *dic in upArray) {
        LJP_searchRecomModel *model = [[LJP_searchRecomModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [model setValue:[dic objectForKey:@"id"] forKey:@"nextId"];
        [self.upArray addObject:model];
    }
    for (NSDictionary *dic in downArray) {
        LJP_searchRecomModel *model = [[LJP_searchRecomModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [model setValue:[dic objectForKey:@"id"] forKey:@"nextId"];
        [self.downArray addObject:model];
    }
    [self collectionViewSubview];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.upArray.count;
    }else{
        return self.downArray.count;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LJP_searchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"qwe" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        cell.name.text = [self.upArray[indexPath.item] name];
    } else {
        cell.name.text = [self.downArray[indexPath.item] name];
    }
    return cell;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    

    DestinationCollectionReusableView  *view = [collectionView  dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"sectionTitle" forIndexPath:indexPath];
    view.title.text = self.titleArray[indexPath.section];
    return view;
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LHQDestinationDetailViewController  *detailTotal = [[LHQDestinationDetailViewController alloc] init];
    LJP_searchRecomModel *model = [[LJP_searchRecomModel alloc] init];
    if (indexPath.section == 0) {
        model = self.upArray[indexPath.item];

    } else {
        model = self.downArray[indexPath.item];
    }
    detailTotal.detailStr = model.nextId;
    detailTotal.type = model.type;
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController  pushViewController:detailTotal animated:YES];
}
//section距离
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 30);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.searchHeader = [[LJP_SearchHeader alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    self.searchHeader.backgroundColor = self.mainColor;
    [self.view addSubview:self.searchHeader];
    [self.searchHeader.cancle addTarget:self action:@selector(cancleClicked) forControlEvents:UIControlEventTouchUpInside];
    self.searchHeader.searchBar.delegate = self;
    [self dataHandle];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.searchHeader.searchBar resignFirstResponder];
    LJP_SearchResultViewController *searchResult = [[LJP_SearchResultViewController alloc] init];
    searchResult.searchText = self.searchHeader.searchBar.text;
    [self.navigationController pushViewController:searchResult animated:YES];
    
}
- (void)cancleClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
